package com.atguigu.jxc.controller;

import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.Goods;
import com.atguigu.jxc.entity.VO.GoodsTypeVO;
import com.atguigu.jxc.service.GoodsService;
import org.apache.catalina.core.ApplicationContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * @description 商品信息Controller
 */
@Controller
public class GoodsController {

    @Override
    public int hashCode() {
        return goodsService != null ? goodsService.hashCode() : 0;
    }

    @Autowired
    private GoodsService goodsService;

    /**
     * 分页查询商品库存信息
     * @param page 当前页
     * @param rows 每页显示条数
     * @param codeOrName 商品编码或名称
     * @param goodsTypeId 商品类别ID
     * @return
     */
    @PostMapping("goods/listInventory")
    @ResponseBody
    public Map<String,Object> listInventory(Integer page, Integer rows, String codeOrName, Integer goodsTypeId) {
        return goodsService.listInventory(page,rows,codeOrName,goodsTypeId);
    }

    /**
     * 查询商品所有分类
     * @return
     */
    @PostMapping("goodsType/loadGoodsType")
    @ResponseBody
    public List<GoodsTypeVO> loadGoodsType() {
        return goodsService.loadGoodsType();
    }

    /**
     * 查询所有的商品单位
     * @return
     */
    @PostMapping("unit/list")
    @ResponseBody
    public Map<String,Object> getUnitList() {
        return goodsService.getUnitList();
    }


    /**
     * 分页查询商品信息
     * @param page 当前页
     * @param rows 每页显示条数
     * @param goodsName 商品名称
     * @param goodsTypeId 商品类别ID
     * @return
     */
    @PostMapping("goods/list")
    @ResponseBody
    public Map<String,Object> queryGoodsList(Integer page, Integer rows, String goodsName, Integer goodsTypeId) {
        return goodsService.queryGoodsList(page,rows,goodsName,goodsTypeId);
    }


    /**
     * 生成商品编码
     * @return
     */
    @PostMapping("goods/getCode")
    @ResponseBody
    // @RequiresPermissions(value = "商品管理")
    public ServiceVO getCode() {
        return goodsService.getCode();
    }

    /**
     * 新增商品分类
     * @return
     */
    @PostMapping("goodsType/save")
    @ResponseBody
    public ServiceVO saveGoodsType(String goodsTypeName,Integer pId) {
        boolean flag = goodsService.saveGoodsType(goodsTypeName,pId);
        if(flag) {
            return new ServiceVO(SuccessCode.SUCCESS_CODE,SuccessCode.SUCCESS_MESS);
        } else {
            return new ServiceVO(-1,"请求失败");
        }
    }

    /**
     * 删除商品分类
     * @return
     */
    @PostMapping("goodsType/delete")
    @ResponseBody
    public ServiceVO deleteGoodsType(Integer goodsTypeId) {
        boolean flag = goodsService.deleteGoodsType(goodsTypeId);
        if(flag) {
            return new ServiceVO(SuccessCode.SUCCESS_CODE,SuccessCode.SUCCESS_MESS);
        } else {
            return new ServiceVO(-1,"请求失败");
        }
    }


    /**
     * 添加或修改商品信息
     * @param goods 商品信息实体
     * @return
     */
    @PostMapping("goods/save")
    @ResponseBody
    public ServiceVO saveGoods(Goods goods) {
        boolean flag = goodsService.saveGoods(goods);
        if(flag) {
            return new ServiceVO(SuccessCode.SUCCESS_CODE,SuccessCode.SUCCESS_MESS);
        } else {
            return new ServiceVO(-1,"请求失败");
        }
    }

    /**
     * 删除商品信息
     * @param goodsId 商品ID
     * @return
     */
    @PostMapping("goods/delete")
    @ResponseBody
    public ServiceVO deleteGoods(Integer goodsId) {
        boolean flag = goodsService.deleteGoods(goodsId);
        if(flag) {
            return new ServiceVO(SuccessCode.SUCCESS_CODE,SuccessCode.SUCCESS_MESS);
        } else {
            return new ServiceVO(-1,"请求失败");
        }
    }

    /**
     * 分页查询无库存商品信息
     * @param page 当前页
     * @param rows 每页显示条数
     * @param nameOrCode 商品名称或商品编码
     * @return
     */
    @PostMapping("goods/getNoInventoryQuantity")
    @ResponseBody
    public Map<String,Object> getNoInventoryQuantity(Integer page,Integer rows,String nameOrCode) {
        return goodsService.getNoInventoryQuantity(page,rows,nameOrCode);
    }

    /**
     * 分页查询有库存商品信息
     * @param page 当前页
     * @param rows 每页显示条数
     * @param nameOrCode 商品名称或商品编码
     * @return
     */
    @PostMapping("goods/getHasInventoryQuantity")
    @ResponseBody
    public Map<String,Object> getHasInventoryQuantity(Integer page,Integer rows,String nameOrCode) {
        return goodsService.getHasInventoryQuantity(page,rows,nameOrCode);
    }


    /**
     * 添加商品期初库存
     * @param goodsId 商品ID
     * @param inventoryQuantity 库存
     * @param purchasingPrice 成本价
     * @return
     */
    @PostMapping("goods/saveStock")
    @ResponseBody
    public ServiceVO saveStock(Integer goodsId,Integer inventoryQuantity,double purchasingPrice) {
        boolean flag = goodsService.saveStock(goodsId,inventoryQuantity,purchasingPrice);
        if(flag) {
            return new ServiceVO(SuccessCode.SUCCESS_CODE,SuccessCode.SUCCESS_MESS);
        } else {
            return new ServiceVO(-1,"请求失败");
        }
    }

    /**
     * 删除商品库存
     * @param goodsId 商品ID
     * @return
     */
    @PostMapping("goods/deleteStock")
    @ResponseBody
    public ServiceVO deleteStock(Integer goodsId) {
        boolean flag = goodsService.deleteStock(goodsId);
        if(flag) {
            return new ServiceVO(SuccessCode.SUCCESS_CODE,SuccessCode.SUCCESS_MESS);
        } else {
            return new ServiceVO(-1,"请求失败");
        }
    }

    /**
     * 查询库存报警商品信息
     * @return
     */
    @PostMapping("goods/listAlarm")
    @ResponseBody
    public Map<String,Object> listAlarm() {
        return goodsService.listAlarm();
    }

}
